Expand description
Strum
Strum is a set of macros and traits for working with enums and strings easier in Rust.
The full version of the README can be found on GitHub.
Including Strum in Your Project
Import strum and strum_macros
into your project by adding the following lines to your
Cargo.toml. strum_macros
contains the macros needed to derive all the traits in Strum.
[dependencies]
strum = "0.25"
strum_macros = "0.25"
# You can also access strum_macros exports directly through strum using the "derive" feature
strum = { version = "0.25", features = ["derive"] }
Modules
- Documentation for Additional Attributes
Enums
- The
ParseError
enum is a collection of all the possible reasons an enum can fail to parse from a string.
Traits
- AsStaticRefDeprecatedA cheap reference-to-reference conversion. Used to convert a value to a reference value with
'static
lifetime within generic code. - A trait for capturing the number of variants in Enum. This trait can be autoderived by
strum_macros
. - Associates additional pieces of information with an Enum. This can be autoimplemented by deriving
EnumMessage
and annotating your variants with#[strum(message="...")]
. EnumProperty
is a trait that makes it possible to store additional information with enum variants. This trait is designed to be used with the macro of the same name in thestrum_macros
crate. Currently, the only string literals are supported in attributes, the other methods will be implemented as additional attribute types become stabilized.- This trait designates that an
Enum
can be iterated over. It can be auto generated usingstrum_macros
on your behalf. - A trait for retrieving the names of each variant in Enum. This trait can be autoderived by
strum_macros
.
Derive Macros
- AsRefStr
derive
Converts enum variants to&'static str
. - AsStaticStr
derive
- Display
derive
Converts enum variants to strings. - EnumCount
derive
Add a constantusize
equal to the number of variants. - EnumDiscriminants
derive
Generate a new type with only the discriminant names. - Generated
is_*()
methods for each variant. E.g.Color.is_red()
. - EnumIter
derive
Creates a new type that iterates of the variants of an enum. - EnumMessage
derive
Add a verbose message to an enum variant. - EnumProperty
derive
Add custom properties to enum variants. - EnumString
derive
Converts strings to enum variants based on their name. - EnumVariantNames
derive
ImplementsStrum::VariantNames
which adds an associated constantVARIANTS
which is an array of discriminant names. - FromRepr
derive
Add a function to enum that allows accessing variants by its discriminant - IntoStaticStr
derive
ImplementsFrom<MyEnum> for &'static str
on an enum. - ToString
derive
implementsstd::string::ToString
on an enum